<?php

namespace Modules\DataAnalysis\Http\Controllers\Admin;

use App\Http\Controllers\Admin\ApiController;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Modules\DataAnalysis\Entities\Comment;
use Modules\DataAnalysis\Entities\CommentPhrase;
use Modules\DataAnalysis\Entities\Setting;
use Modules\DataAnalysis\Entities\Video;
use Modules\DataAnalysis\Http\Requests\Admin\DataStatisticRequest;

class DataStatisticController extends ApiController
{
    /**
     * 说明：积极消极比例
     *
     * @return
     * @param   type
     * @author  daixinguo (daixinguo57@163.com)
     */
    public function feelingRatio(DataStatisticRequest $request)
    {
        // 系统设置
        $settings = Setting::query()->get();
        $score_line = $settings->where('key', 'score_line')->first()->value;

        $positive_comments_count = Comment::query()->where('video_id', $request->get('video_id'))->where('score', '>=', $score_line)->count();
        $passive_comments_count = Comment::query()->where('video_id', $request->get('video_id'))->where('score', '<', $score_line)->count();
        $positive_ratio = 0;
        $passive_ratio = 0;
        $total = $positive_comments_count + $passive_comments_count;
        if ($total > 0) {
            $positive_ratio = round(($positive_comments_count / $total) * 100, 2);
            $passive_ratio = round(($passive_comments_count / $total) * 100, 2);
        }
        return $this->success([
            'positive_comments_count' => $positive_comments_count,
            'passive_comments_count' => $passive_comments_count,
            'positive_ratio' => $positive_ratio,
            'passive_ratio' => $passive_ratio,
        ]);
    }

    /**
     * 说明：评论情绪分值散点图
     *
     * @return
     * @param   type
     * @author  daixinguo (daixinguo57@163.com)
     */
    public function feelingScatterDiagram(DataStatisticRequest $request)
    {
        // 系统设置
        $settings = Setting::query()->get();
        $score_line = $settings->where('key', 'score_line')->first()->value;

        // 分男女两个群体的数据给
        $video_id = $request->get('video_id');
        $male_comment = Comment::query()->select('id', 'score')->where('video_id', $video_id)->where('sex', 1)->get()->toArray();
        $male_comment_data = [];
        foreach ($male_comment as $key => $value) {
            array_push($male_comment_data, [
                $key + 1, $value['score']
            ]);
        }
        $female_comment = Comment::query()->select('id', 'score')->where('video_id', $video_id)->where('sex', 2)->get()->toArray();
        $female_comment_data = [];
        foreach ($female_comment as $key => $value) {
            array_push($female_comment_data, [
                $key + 1, $value['score']
            ]);
        }

        return $this->success([
            'score_line' => $score_line,
            'male_comment' => $male_comment_data,
            'female_comment' => $female_comment_data,
        ]);
    }

    /**
     * 说明：男女比例统计图
     *
     * @return
     * @param   type
     * @author  daixinguo (daixinguo57@163.com)
     */
    public function feelingMaleFemaleRatio(DataStatisticRequest $request, $feeling)
    {
        // 系统设置
        $settings = Setting::query()->get();
        $score_line = $settings->where('key', 'score_line')->first()->value;

        $video_id = $request->get('video_id');
        $query = Comment::query()->where('video_id', $video_id);
        if ($feeling == 'positive') {
            // 积极
            $operator = '>=';
        } else {
            // 消极
            $operator = '<';
        }

        $video_comments = $query->where('score', $operator, $score_line)->get();
        // 男性评论
        $male_comment_count = $video_comments->where('sex', 1)->count();
        // 女性评论
        $female_comment_count = $video_comments->where('sex', 2)->count();

        return $this->success([
            [
                'value' => $male_comment_count,
                'name' => '男',
            ],
            [
                'value' => $female_comment_count,
                'name' => '女',
            ]
        ]);
    }

    /**
     * 说明：积极或消极评论地图分布
     *
     * @return
     * @param   type
     * @author  daixinguo (daixinguo57@163.com)
     */
    public function feelingMapScattergram(DataStatisticRequest $request, $feeling)
    {
        // 各省省会经纬度
        $geoCoordMap = [
            '安徽' => ['117.231654', '31.828344'],
            '北京' => ['116.404763', '39.916901'],
            '福建' => ['119.30992', '26.085315'],
            '甘肃' => ['103.8342', '36.070834'],
            '广东' => ['113.260353', '23.149912'],
            '广西' => ['108.370167', '22.832727'],
            '贵州' => ['106.633985', '26.656993'],
            '海南' => ['110.34519', '20.032594'],
            '河北' => ['114.515322', '38.055703'],
            '河南' => ['113.628874', '34.763885'],
            '黑龙江' => ['126.539725', '45.814103'],
            '湖北' => ['114.311648', '30.607067'],
            '湖南' => ['112.947635', '28.240793'],
            '吉林' => ['125.326833', '43.829127'],
            '江苏' => ['118.798874', '32.06947'],
            '江西' => ['115.857929', '28.696139'],
            '辽宁' => ['123.467214', '41.694045'],
            '内蒙古' => ['111.749609', '40.85525'],
            '宁夏' => ['106.24491', '38.508734'],
            '青海' => ['101.786458', '36.631097'],
            '山东' => ['117.118854', '36.665362'],
            '山西' => ['112.546379', '37.886894'],
            '陕西' => ['108.940842', '34.35333'],
            '上海' => ['121.479965', '31.24408'],
            '四川' => ['104.074038', '30.586249'],
            '成都' => ['104.075045', '30.600134'],
            '天津' => ['117.21729', '39.006595'],
            '西藏' => ['91.123769', '29.660176'],
            '新疆' => ['87.621402', '43.834414'],
            '云南' => ['102.843476', '24.901416'],
            '浙江' => ['120.160687', '30.295251'],
            '香港' => ['114.210263', '22.288013'],
            '澳门' => ['113.54814', '22.17308'],
            '台湾' => ['120.650074', '24.167939'],
        ];

        // 系统设置
        $settings = Setting::query()->get();
        $score_line = $settings->where('key', 'score_line')->first()->value;

        $video_id = $request->get('video_id');
        $query = Comment::query()->where('video_id', $video_id);
        if ($feeling == 'positive') {
            // 积极
            $operator = '>=';
        } else {
            // 消极
            $operator = '<';
        }

        $video_comments = $query->where('score', $operator, $score_line)->get();
        $data = [];
        foreach ($geoCoordMap as $key => $value) {
            $item = [
                'name' => $key,
                'value' => $video_comments->where('district', $key)->count()
            ];
            array_push($data, $item);
        }

        return $this->success([
            'data' => $data,
            'geo_coord_map' => $geoCoordMap
        ]);
    }

    /**
     * 说明：积极或消极评论词云图
     *
     * @return
     * @param   type
     * @author  daixinguo (daixinguo57@163.com)
     */
    public function feelingWordCloud(DataStatisticRequest $request, $feeling)
    {
        // 系统设置
        $settings = Setting::query()->get();
        $score_line = $settings->where('key', 'score_line')->first()->value;

        $video_id = $request->get('video_id');
        $query = Comment::query()->where('video_id', $video_id);
        if ($feeling == 'positive') {
            // 积极
            $operator = '>=';
        } else {
            // 消极
            $operator = '<';
        }

        $video_comment_ids = $query->where('score', $operator, $score_line)->get()->pluck('id')->toArray();
        $comment_phrases = CommentPhrase::query()->where('video_id', $video_id)->whereIn('comment_id', $video_comment_ids)->get();
        $comment_phrases = $comment_phrases->groupBy('phrase');

        $data = [];
        foreach ($comment_phrases as $key => $value) {
            $count = $value->count();
            if ($count > 1) {
                $item = [
                    'name' => $key,
                    'value' => $count
                ];
                array_push($data, $item);
            }
        }
        return $this->success($data);
    }
}
